<template>
	<div class="app-container">
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
					<div class="flex-start-center" style="font-size: 14px;">
						<el-button size="mini">
							<div class="flex-start-center">
								<div style="width: 8px;height: 8px;border-radius: 50%;background: #2b85e4;margin-right: 3px;"></div>
								<span>开台中</span>
							</div>
						</el-button>
						<el-button size="mini">
							<div class="flex-start-center">
								<div style="width: 8px;height: 8px;border-radius: 50%;background: #808695;margin-right: 3px;"></div>
								<span>待开台</span>
							</div>
						</el-button>
					</div>
					<!--<el-button class="margin-5-0 item-gap" type="primary" size="small" @click="onAddBtn">批量排产</el-button>-->
				</div>
				<div class="flex-end-center-wrap">
					<jk-select v-model="searchParams.workshopId" :option-list="workshopList" :clearable="false" placeholder="区域" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>
					<!--<jk-select v-model="searchParams.auditState" :option-list="auditStateList" placeholder="状态" clearable class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>-->
					<jk-search-input v-model="searchParams.productNameOrCode" placeholder="品种编号或名称" size="small" class="search-bar-item-size2 margin-5-0 item-gap" clearable @on-enter="onSearchBtn" />
					<jk-search-input v-model="searchParams.batchCode" placeholder="批号" size="small" class="search-bar-item-size2 margin-5-0 item-gap" clearable @on-enter="onSearchBtn" />
					<jk-search-input v-model="searchParams.machineNameOrCode" placeholder="机台编号或名称" size="small" class="search-bar-item-size2 margin-5-0 item-gap" clearable @on-enter="onSearchBtn" />
					<jk-search-button @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</jk-operation-card>

		<jk-card>
			<jk-table ref="jkTable" :stripe="false" :span-method="mergeColMethod" border :loading="tableLoading" :data="tableData" :height="tableHeight + 30">
				<vxe-column
					v-for="(item, index) in tableColumnList"
					:key="index"
					:title="item.title"
					:align="item.align"
					:field="item.keyName"
					:sortable="item.sortable"
					:fixed="item.fixed ? item.fixed : ''"
					:min-width="item.minWidth"
					:width="item.width"
				>
					<template v-if="item.keyName === 'productTaskList'" slot="header">
						<el-row class="width-100-percent" style="padding: 0 24px 0 32px;">
							<el-col :span="6">品种</el-col>
							<el-col :span="5">批号</el-col>
							<el-col :span="4">细纱管</el-col>
							<el-col :span="4">纸管</el-col>
							<el-col :span="5">细纱机</el-col>
						</el-row>
					</template>
					<template slot-scope="scope">
						<jk-common-status v-if="item.keyName === 'auditState'" :status="scope.row.auditState"></jk-common-status>
						<div v-else-if="item.keyName === 'machineName'">
							<el-button size="small" type="default">{{ scope.row.machineName }}</el-button>
						</div>
						<div v-else-if="item.keyName === 'startSpinNumber'">
							<span>{{ scope.row.startSpinNumber ? `${scope.row.startSpinNumber}~${scope.row.endSpinNumber}` : '' }}</span>
						</div>
						<div v-else-if="item.keyName === 'empList'">
							<span>{{ scope.row.empList ? scope.row.empList.map(x => x.empName).join(',') : '' }}</span>
						</div>
						<div v-else-if="item.keyName === 'productTaskList'">
							<el-row :gutter="10">
								<el-col
									v-for="(productItem, productIndex) in scope.row.productTaskList"
									:key="productIndex"
									:span="24"
								>
									<el-tag :type="productItem.openingState === 2 ? 'info' : 'primary'" effect="dark" closable class="width-100-percent flex-between-center" style="margin: 2px 0;" @close="onDeleteBtn(productItem)">
										<div class="flex-start-center width-100-percent">
											<div
												v-if="productItem.openingState === 2"
												slot="reference"
												class="el-icon-video-play open-icon operation-product-icon"
												@click="onOperationBtn(scope, productItem)"
											></div>
											<el-popconfirm
												v-else
												confirm-button-text="了机"
												cancel-button-text="取消开台"
												icon="el-icon-info"
												title="你可以进行以下操作~"
												@confirm="onOperationBtn(scope, productItem)"
												@cancel="onCancelBtn(scope, productItem)"
											>
												<div slot="reference" style="margin-top: 6px;" class="el-icon-video-pause close-icon operation-product-icon"></div>
											</el-popconfirm>
											<el-row class="width-100-percent">
												<el-col :span="6">

													<span>{{ productItem.productName }}</span>
												</el-col>
												<el-col :span="5">{{ productItem.batchCode }}</el-col>
												<el-col :span="4">{{ productItem.spunYarnColors && productItem.spunYarnColors.length ? productItem.spunYarnColors.map(x => x.name).join() : '无' }}</el-col>
												<el-col :span="4">{{ productItem.paperTubeList && productItem.paperTubeList.length ? productItem.paperTubeList.map(x => x.name).join() : '无' }}</el-col>
												<el-col :span="5" style="white-space: nowrap;text-overflow: ellipsis;overflow: hidden;">
													<el-popover
														title="机台明细"
														width="500"
														trigger="click"
														content=""
													>
														<div class="flex-start-center-wrap">
															<div
																v-for="(machineItem, machineIndex) in productItem.prdNoticeMachineList"
																:key="machineIndex"
																style="width: calc(100% / 5);padding: 2px;"
															>
																<el-tag class="width-100-percent">{{ machineItem.machineCode }}</el-tag>
															</div>
														</div>
														<span slot="reference">{{ productItem.prdNoticeMachineList && productItem.prdNoticeMachineList.length ? productItem.prdNoticeMachineList.map(x => x.machineCode).join() : '无' }}</span>
													</el-popover>
												</el-col>
											</el-row>
										</div>
									</el-tag>
								</el-col>
							</el-row>
						</div>
						<span v-else>{{ scope.row[item.keyName] }}</span>
					</template>
				</vxe-column>
				<vxe-column title="操作" align="center" fixed="right" :width="70">
					<template slot-scope="scope">
						<el-tag style="margin: 2px 0;cursor: pointer;color: #2d8cf0;" type="info" effect="plain" @click="onAddSingleTaskBtn(scope)">
							<div class="el-icon-plus"></div>
						</el-tag>
					</template>
				</vxe-column>
			</jk-table>
		</jk-card>
		<detail-dialog
			:data-id="dataId"
			:dialog-state="detailDialogState"
			@on-visible-change="onVisibleChangeOfDetailDialog"
		></detail-dialog>
		<batch-create-dialog
			:permission-prefix="prefix"
			:data-id="dataId"
			:dialog-state="batchCreateDialogState"
			@on-visible-change="onVisibleChangeOfBatchCreate"
			@on-finish="onFinish"
		></batch-create-dialog>
		<single-create-dialog
			:permission-prefix="prefix"
			:row-data="rowData"
			:dialog-state="singleCreateDialogState"
			@on-visible-change="onVisibleChangeOfSingleCreateDialog"
			@on-finish="onFinishOfSingleCreateDialog"
		></single-create-dialog>
		<open-dialog
			:select-machine-item="selectMachineItem"
			:dialog-state="openDialogState"
			@on-visible-change="onVisibleChangeOfOpenDialog"
			@on-confirm="onConfirmChangeOfOpenDialog"
		></open-dialog>
		<close-dialog
			:select-machine-item="selectMachineItem"
			:dialog-state="closeDialogState"
			@on-visible-change="onVisibleChangeOfCloseDialog"
			@on-confirm="onConfirmChangeOfCloseDialog"
		></close-dialog>
	</div>
</template>

<script>
    import { auditStateList } from '@/utils/jk/status-option-list';
    import batchCreateDialog from './batch-create-dialog';
    import singleCreateDialog from './single-create-dialog';
    import detailDialog from './detail-dialog';
    import calHeight from '/src/mixins/cal-table-height';
    import { pageConfig } from '/src/utils/jk/jk-tool';
    import {
        productionPrdNoticeLtMachineChangeClearOpening,
        productionPrdNoticeLtMachineChangeDelete,
        productionPrdNoticeLtMachineChangeGetMachineList
    } from '../../../../api/production/ltNotice';
    import getMenuColumn from '../../../../mixins/getMenuColumn';
    import handlerCache from '../../../../mixins/handler-cache';
    import openDialog from './open-dialog';
    import closeDialog from './close-dialog';

    export default {
        name: 'LtMachineList',
        components: {
            closeDialog,
            openDialog,
            batchCreateDialog,
            singleCreateDialog,
            detailDialog
        },
        mixins: [calHeight, getMenuColumn, handlerCache],
        data() {
            return {
                selectMachineItem: {},
                prefix: 'production:prdNoticeLtMachineChange',
                apiItem: {
                    'page': productionPrdNoticeLtMachineChangeGetMachineList,
                    'delete': productionPrdNoticeLtMachineChangeDelete
                },
                workshopList: [],
                singleCreateDialogState: false,
                importDialogState: false,
                auditStateList: auditStateList,
                detailDialogState: false,
                dataId: null,
                batchCreateDialogState: false,
                tableLoading: true,
                exportLoading: false,
                tableData: [],
                searchParams: {
                    count: 0,
                    pageNo: pageConfig.pageNo,
                    pageSize: 1000,
                    machineNameOrCode: null,
                    workshopId: null,
                    batchCode: '',
                    productNameOrCode: '',
                    typeId: null,
                    auditState: null
                },
                rowData: {},
                closeDialogState: false,
                openDialogState: false
            };
        },
        methods: {
            onVisibleChangeOfCloseDialog(e) {
                this.closeDialogState = e;
            },
            onConfirmChangeOfCloseDialog() {
                this.closeDialogState = false;
                this.getListRequest();
            },
            onDeleteBtn(item) {
                this.$confirm(`确定"删除"吗？`, '提示', { type: 'warning' }).then(() => {
                    productionPrdNoticeLtMachineChangeDelete({
                        id: item.id
                    }).then(res => {
                        if (res.data.status === 200) {
                            this.getListRequest();
                        }
                    });
                }).catch(() => {});
            },
            onConfirmChangeOfOpenDialog() {
                this.openDialogState = false;
                this.getListRequest();
            },
            onVisibleChangeOfOpenDialog(e) {
                this.openDialogState = e;
            },
            onFinishOfSingleCreateDialog() {
                this.singleCreateDialogState = false;
                this.getListRequest();
            },
            onCancelBtn(scope, item) {
                if (item.openingState === 3) {
                    productionPrdNoticeLtMachineChangeClearOpening({
                        prdNoticeMachineChangeId: item.id
                    }, {
                        prdNoticeMachineChangeId: item.id
                    }).then(res => {
                        if (res.data.status === 200) {
                            this.getListRequest();
                        }
                    });
                }
            },
            onOperationBtn(scope, productItem) {
                this.selectMachineItem = {
                    workshopId: scope.row.workshopId,
                    workshopCode: scope.row.workshopCode,
                    workshopName: scope.row.workshopName,
                    machineId: scope.row.machineId,
                    machineCode: scope.row.machineCode,
                    machineName: scope.row.machineName,
                    startSpinNumber: scope.row.startSpinNumber,
                    endSpinNumber: scope.row.endSpinNumber,
                    spinGroupId: productItem.spinGroupId,
                    ...productItem
                };
                if (productItem.openingState === 2) {
                    this.openDialogState = true;
                } else if (productItem.openingState === 3) {
                    this.closeDialogState = true;
                }
            },
            onAddSingleTaskBtn(scope) {
                this.rowData = scope.row;
                this.singleCreateDialogState = true;
            },
            onVisibleChangeOfSingleCreateDialog(e) {
                this.singleCreateDialogState = e;
            },
            mergeColMethod({ row, _rowIndex, column, visibleData }) {
                const fields = ['machineName']; // 根据字段判断数据相同的合并
                const cellValue = row[column.property];
                if (cellValue && fields.includes(column.property)) {
                    const prevRow = visibleData[_rowIndex - 1];
                    let nextRow = visibleData[_rowIndex + 1];
                    if (prevRow && prevRow[column.property] === cellValue) {
                        return { rowspan: 0, colspan: 0 };
                    } else {
                        let countRowspan = 1;
                        while (nextRow && nextRow[column.property] === cellValue) {
                            nextRow = visibleData[++countRowspan + _rowIndex];
                        }
                        if (countRowspan > 1) {
                            return { rowspan: countRowspan, colspan: 1 };
                        }
                    }
                }
            },
            onVisibleChangeOfImportDialog(e) {
                this.importDialogState = e;
            },
            onFinishBtnOfImportDialog() {
                this.importDialogState = false;
                this.getListRequest();
            },
            onPageChange(pageIndex, pageSize) {
                this.searchParams.pageNo = pageIndex;
                this.searchParams.pageSize = pageSize;
                this.getListRequest();
            },
            onDetailBtn(e) {
                this.dataId = e.id;
                this.detailDialogState = true;
            },
            onVisibleChangeOfDetailDialog(e) {
                if (!e) {
                    this.dataId = null;
                }
                this.detailDialogState = e;
            },
            onFinish() {
                this.batchCreateDialogState = false;
                this.getListRequest();
            },
            onVisibleChangeOfBatchCreate(e) {
                if (!e) {
                    this.dataId = null;
                }
                this.batchCreateDialogState = e;
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                this.apiItem.page(this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.tableData = res.data.res;
                        this.searchParams.count = res.data.count;
                    }
                });
            },
            /** 搜索按钮操作 */
            onSearchBtn() {
                this.searchParams.pageNo = 1;
                this.searchParams.count = 1;
                this.getListRequest();
            },
            /** 新增按钮操作 */
            onAddBtn() {
                this.batchCreateDialogState = true;
            },
            /** 修改按钮操作 */
            onEditBtn(row) {
                this.dataId = row.id;
                this.batchCreateDialogState = true;
            },
            clearCacheMethod() {
                // Object.assign(this.$data, this.$options.data());
                this.$nextTick(() => {
                    this.getDependentData();
                });
            },
            async getDependentData() {
                await this.getMenuColumnListRequest();
                await this.getListRequest();
            }
        },
        mounted() {
            this.workshopList = this.$store.state.user.userWorkshop.workshopList;
            this.$set(this.searchParams, 'workshopId', this.$store.state.user.userWorkshop.defaultWorkshopId);
            this.getDependentData();
        }
    };
</script>
<style scoped lang="scss">
    .operation-product-icon {
        font-size: 18px;
        font-weight: bold;
        cursor: pointer;
        margin-right: 4px;
    }
    .operation-product-icon:hover {
        color: #fff;
    }
    .product-item-label {
        color: #5cadff;
        margin-right: 4px;
    }
    .product-item-value {
        color: #2b85e4;
        font-weight: 500;
    }
    .open-icon {
        color: #515a6e;
    }
    .close-icon {
        color: blue;
    }
</style>
